package com.service.business.support;

import com.service.business.base.constants.RedisLockConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年11月26日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Component
public class TradingSpotLockUtils extends RedissonLockUtils {


    @Override
    public boolean lock(@NotNull String key) {
        return super.lock(key);
    }

    @Override
    public void unlock(@NotNull String key) {
        super.unlock(key);
    }

    public boolean lockMember(@NotNull Integer memberId) {
        return this.lock(buildLockMember(memberId));
    }

    public void unlockMember(@NotNull Integer memberId) {
        this.unlock(buildLockMember(memberId));
    }

    public static String buildLockMember(Integer memberId) {
        return RedisLockConstant.TRADING_SPOT_PRE
                + RedisLockConstant.MEMBER_PRE + memberId;
    }

    public boolean lockOrder(@NotNull Integer orderId) {
        return this.lock(buildLockOrderKey(orderId));
    }

    public void unlockOrder(@NotNull Integer orderId) {
        this.unlock(buildLockOrderKey(orderId));
    }

    public static String buildLockOrderKey(Integer orderId) {
        return RedisLockConstant.TRADING_SPOT_PRE
                + RedisLockConstant.ORDER + orderId;
    }

}
